package cn.itcast.goods.user.service;

import java.io.IOException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;

import cn.itcast.commons.CommonUtils;
import cn.itcast.goods.user.dao.UserDao;
import cn.itcast.goods.user.domain.User;
import cn.itcast.goods.user.service.exception.UserException;
import cn.itcast.mail.Mail;
import cn.itcast.mail.MailUtils;

/**
 * 用户模块业务层
 * @author GiantTao
 *
 */
public class UserService {
	private UserDao userDao=new UserDao();
	
	public void updatePassword(String uid,String loginpass,String newPass) throws UserException{
		try {
			/**
			 * 1.校验老密码
			 */
			boolean bool=userDao.getByUidAndPass(uid, loginpass);
			if(!bool){
				throw new UserException("原密码错误！");
			}
			/**
			 * 2.修改密码
			 */
			userDao.updatePassword(uid, newPass);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * 登录功能
	 * @param user
	 * @return
	 */
	public User login(User user){
		try {
			return userDao.getUserByLoginnameAndLoginpass(user.getLoginname(), user.getLoginpass());
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	public void activation(String code) throws UserException{
		/**
		 * 1.通过激活码激活用户
		 * 2.如果用户为null，说明是无效激活码，抛出异常，给出异常信息（无效的激活码）
		 * 3.查看用户状态是否为true,如果为true，抛出异常，给出异常信息（请不要二次激活）
		 * 4.修改用户状态为true
		 */
		try {
			User user=userDao.getUserByActivationCode(code);
			if(user==null) throw new UserException("无效的激活码!");
			if(user.getStatus()==1) throw new UserException("请不要二次激活！");
			userDao.updateStatus(user.getUid(), 1);//修改状态
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
	}
	/**
	 * 用户名注册校验
	 * @param loginname
	 * @return
	 */
	public boolean ajaxValidateLoginname(String loginname) {
		try {
			return userDao.ajaxValidateLoginname(loginname);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 邮箱校验
	 * @param email
	 * @return
	 */
	public boolean ajaxValidateEmail(String email) {
		try {
			return userDao.ajaxValidateEmail(email);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 注册功能
	 * @param user
	 */
	public void regist(User user){
		/**
		 * 1.数据的补齐
		 */
		user.setUid(CommonUtils.uuid());
		user.setStatus(0);
		user.setActivationCode(CommonUtils.uuid()+CommonUtils.uuid());
		/**
		 * 2.向数据库插入数据
		 */
		try {
			userDao.add(user);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		/**
		 * 3.发送邮件
		 */
		/**
		 * 登录邮件服务器，创建session,使用配置文件中的属性值
		 */
		Properties properties=new Properties();
		try {
			properties.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties"));
		} catch (IOException e1) {
			throw new RuntimeException(e1);
		}
		String host=properties.getProperty("host");//服务器主机名
		String name=properties.getProperty("username");//用户名
		String pass=properties.getProperty("password");//密码
		Session session=MailUtils.createSession(host, name, pass);
		/**
		 * 创建Mail对象
		 */
		String from=properties.getProperty("from");
		String to=user.getEmail();
		String subject=properties.getProperty("subject");
		//MessageFormat:降低一个参数中的｛｝中的内容一次替换为第二个参数。。。
		//如MessageFormat("你好｛0｝,你｛1｝","张三"，"去死吧");会返回你好张三，你去死吧
		String content=MessageFormat.format(properties.getProperty("content"), user.getActivationCode());
		Mail mail=new Mail(from,to,subject,content);
		/**
		 * 发送邮件
		 */
		try {
			MailUtils.send(session, mail);
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		
	}
}
